//*****************************************************************************
//
// graphics.c - Simple Graphics Display Example
//
// Copyright (c) 2006-2012 Texas Instruments Incorporated.  All rights reserved.
// Software License Agreement
// 
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
// 
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
// 
// This is part of revision 8555 of the EK-LM3S8962 Firmware Package.
//
//*****************************************************************************

#include "hw_types.h"
#include "debug.h"
#include "sysctl.h"
#include "rit128x96x4.h"
#include "main.h"
//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>Graphics Example (graphics)</h1>
//!
//! A simple application that displays scrolling text on the top line of the
//! OLED display, along with a 4-bit gray scale image.
//
//*****************************************************************************

//*****************************************************************************
//
// The size of the header on the bitmap image. For a typical 4bpp Windows
// bitmap this is 0x76 bytes comprising the total number of bytes in the
// bitmap file header, the bitmap info header and the palette.
//
//*****************************************************************************
#define BITMAP_HEADER_SIZE   0x76

//*****************************************************************************
//
// The byte offsets into the image at which we can find the height and width.
// Each of these values are encoded in 4 bytes.
//
//*****************************************************************************
#define BITMAP_WIDTH_OFFSET  0x12
#define BITMAP_HEIGHT_OFFSET 0x16

//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif

void drawBmpImage(unsigned char* imagePtr)
{
	unsigned long ulRow, ulWidth, ulHeight;
	unsigned char *pucRow;

    //
    // Extract the width and height from the bitmap data.  These are encoded
    // in 4 byte fields but this application can't support images wider than
    // 128 pixels or taller than 80 rows so we merely read the least
    // significant byte.
    //
    ulHeight = (unsigned long)imagePtr[BITMAP_HEIGHT_OFFSET];
    ulWidth = (unsigned long)imagePtr[BITMAP_WIDTH_OFFSET];

    //
    // Display the BMP Image.
    //
    // The image is an ulWidth by ulHeight 4-bit gray scale image in BMP
    // format.  The divides by two are to account for the fact that there
    // are 2 pixels in each byte of image data.
    //

    //
    // Get a pointer to the first row of pixels in the image (which maps to the
    // bottom row on the display since bitmaps are encoded upside down).
    //
    pucRow = (unsigned char *)&imagePtr[BITMAP_HEADER_SIZE];

    for(ulRow = 0; ulRow < ulHeight; ulRow++)
        {
            //
            // Display in reverse row order.  We center the image horizontally on
            // the display.
            //
            RIT128x96x4ImageDraw(pucRow, 0,
                                 98-ulRow, ulWidth, 1);

            //
            // Move to the next row in the source image.
            //
            pucRow += (ulWidth / 2);
        }
}

void delayTime(int timer1, int timer2)
{
	int i = 0;
	int j = 0;
	for(i = 0; i < timer1; i++)
	{
		for(j = 0; j< timer2; j++)
		{

		}
	}
}

int main(void)
{
    //
    // Set the clocking to run directly from the crystal.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_8MHZ);

    //
    // Initialize the OLED display.
    //
    RIT128x96x4Init(1000000);

    volatile tBoolean displayImage = 0x0;
    unsigned char * imagePtr;
    while(1)
    {
    	imagePtr = (displayImage == 0x0) ? &imageBaby[0] : &imageJeep[0];
    	drawBmpImage(imagePtr);
    	delayTime(10000,1000);
    	RIT128x96x4Clear();
    	displayImage = ~displayImage;
    }
}
